<?php

/**
 * private_messages actions.
 *
 * @package    sf_sandbox
 * @subpackage private_messages
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class private_messagesActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeShow(sfWebRequest $request)
  {
   $this->user_id = $user_id = $this->getUser()->getGuardUser()->getId(); 
   $q = Doctrine_Query::create()
  ->from('PrivateMessages pm')
  ->where('pm.sender_id = ?', $user_id)
  ->orWhere('pm.receiver_id = ?',$user_id)
  ->orderBy('pm.id DESC');
   $this->messages = $q->fetchArray();
   
   // Flag Viewed
   /* $q = Doctrine_Query::create()
        ->update('PrivateMessages pm')
        ->set('pm.is_viewed','?','1')
        ->where('pm.sender_id = ?', $user_id)
        ->orWhere('pm.receiver_id = ?',$user_id)
        ->execute();*/
  }
  public function executeShowFullMessage(sfWebRequest $request)
  {
   $this->user_id = $user_id = $this->getUser()->getGuardUser()->getId(); 
   $id = $request->getParameter('msg_id');
   $q = Doctrine_Query::create()
  ->from('PrivateMessages pm')
  ->where('pm.id = ?', $id);
   $message = $q->fetchOne();
   
   if($message['receiver_id'] == $user_id ){
       // Flag Viewed
    $q = Doctrine_Query::create()
        ->update('PrivateMessages pm')
        ->set('pm.is_viewed','?','1')
        ->where('pm.id = ?', $id)
        ->execute();
        return $this->renderText($message['body']);
   }elseif($message['sender_id'] == $user_id){
       return $this->renderText($message['body']);
   }
   return $this->renderText('You can only edit your messages!.');
  }
  public function executeAdd(sfWebRequest $request)
  {
      $text = $request->getParameter('private_messages_body');
      if(empty ($text)){
          $this->error = I18nUtilities::__("Please enter the private messages body.");
          return $this->renderText($this->error);
      }
      $obj = new PrivateMessages();
      $obj->setSenderId($this->getUser()->getGuardUser()->getId());
      $obj->setReceiverId(1);
      $obj->setBody($text);
      $obj->save();
      $this->error = I18nUtilities::__("your private messages added successfuly.");
      return $this->renderText($this->error);
  }
}
